// RAF动画的真实间隔时间，可以更好的实现匀速动画
export function RAFTime(callback) {
  let T0 = new Date(); //上次时间
  function render() {
    let T1 = new Date(); //本次时间
    let t = T1 - T0; //时间差
    T0 = T1; //把本次时间赋值给上次时间
    callback(t);
    requestAnimationFrame(render);
  }
  return render;
}

// 球体网格模型创建函数
export function sphereMesh(R, x, y, z, isCastShadow = true) {
  var geometry = new THREE.SphereGeometry(R, 25, 25); //球体几何体
  var material = new THREE.MeshPhongMaterial({
    color: 0x0000ff,
  }); //材质对象Material
  var mesh = new THREE.Mesh(geometry, material); // 创建网格模型对象
  mesh.position.set(x, y, z);
  mesh.castShadow = isCastShadow;
  return mesh;
}

// 圆柱体网格模型创建函数
export function cylinderMesh(R, h, x, y, z, isCastShadow = true) {
  var geometry = new THREE.CylinderGeometry(R, R, h, 25, 25); //球体几何体
  var material = new THREE.MeshPhongMaterial({
    color: 0x0000ff,
  }); //材质对象Material
  var mesh = new THREE.Mesh(geometry, material); // 创建网格模型对象
  mesh.position.set(x, y, z);
  mesh.castShadow = isCastShadow;
  return mesh;
}

// Vector2数据,转为Vector3
export function v2ToV3(points) {
  // 2维向量 转 3维向量
  var points3D = [];
  points.forEach((p) => {
    points3D.push(new THREE.Vector3(p.x, p.y, p.z || 0));
  });
  return points3D;
}
